//
//  OrderCenterVC.swift
//  Employee
//
//  Created by swordjoy on 2017/3/24.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class OrderCenterVC: BaseVC {
  
  fileprivate var allOrderVC: OrderStateVC!
  fileprivate var acceptingOrderVC: OrderStateVC!
  fileprivate var submitOrderVC: OrderStateVC!
  fileprivate var waitOrderVC: OrderStateVC!
  
  fileprivate var cacheIndexs = Set<Int>()
  
  fileprivate var ant: SJAnt!
  
  override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = GlobalConfig.BGWhiteColor
    title = "工单中心"
    
    create()
  }
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    GlobalConfig.baiduStatisticsPageViewStart("工单中心")
  }
  
  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    GlobalConfig.baiduStatisticsPageViewEnd("工单中心")
  }
  
}

// MARK: - action

extension OrderCenterVC {

  @objc fileprivate func searchAction() {
    let vc = OrderSearchVC()
    navigationController?.pushViewController(vc, animated: true)
  }
  
}

// MARK: - fileprivate

extension OrderCenterVC {
  
  fileprivate func create() {
    
    allOrderVC = OrderStateVC()
    allOrderVC.title = "全部工单"
    allOrderVC.parentVC = self
    allOrderVC.type = .all
    
    acceptingOrderVC = OrderStateVC()
    acceptingOrderVC.title = "我受理中"
    acceptingOrderVC.parentVC = self
    acceptingOrderVC.type = .accepting
    
    submitOrderVC = OrderStateVC()
    submitOrderVC.title = "我提交的"
    submitOrderVC.parentVC = self
    submitOrderVC.type = .submit
    
    waitOrderVC = OrderStateVC()
    waitOrderVC.title = "待接工单"
    waitOrderVC.parentVC = self
    waitOrderVC.type = .wait
    
    let VCs = [allOrderVC, acceptingOrderVC, submitOrderVC, waitOrderVC]
    
    var style = SJAntBarTitleStyle()
    style.color = UIColor.hex("323C49").withAlphaComponent(0.4)
    let frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.noNavBarHeight)
    ant = SJAnt(frame: frame, VCs: VCs as! [UIViewController], parentVC: self, barStyle:style, delegate: self)
    ant.backgroundColor = UIColor.white
    view.addSubview(ant)
    
    addBackItem()
    
    let backItem = UIBarButtonItem.init(image: UIImage(named:"search-icon-blue")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(searchAction))
    navigationItem.rightBarButtonItem = backItem
  }
}

extension OrderCenterVC: SJAntDelegate {
  func SJAntIndexChange(index: Int) {
    update(with: index)
  }
  
  fileprivate func update(with index: Int) {
    
    switch index {
    case 0:
      self.allOrderVC.updateData()
    case 1:
      self.acceptingOrderVC.updateData()
    case 2:
      self.submitOrderVC.updateData()
    case 3:
      self.waitOrderVC.updateData()
      
    default: break
      
    }
  }
  
}
